package com.onlinelibrary.activities.welcome;
import android.annotation.SuppressLint;
import android.app.Application;
import android.content.Intent;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.databinding.BindingAdapter;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;
import com.bumptech.glide.Glide;
import com.onlinelibrary.activities.main.MainActivity;
import com.onlinelibrary.utils.Http;
import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
public class WelcomeVM extends AndroidViewModel {
    public static MutableLiveData<String> skip=new MutableLiveData<>(),brand=new MutableLiveData<>(),img=new MutableLiveData<>();
    @SuppressLint("StaticFieldLeak")
    private static WelcomeActivity welcomeActivity;
    int recLen = 2;
    Handler handler=new Handler();
    Runnable runnable=new Runnable(){
        @Override
        public void run() {
            welcomeActivity.runOnUiThread(() -> {
                skip.setValue("跳过("+recLen+")");
                recLen--;
                if (recLen<0){
                    skip.setValue("启动中...");
                    welcomeActivity.startActivity(new Intent(welcomeActivity, MainActivity.class));
                    welcomeActivity.finish();
                    handler.removeCallbacks(runnable);
                }
            });
            handler.postDelayed(this, 2000);
        }
    };
    public WelcomeVM(@NonNull Application application) {
        super(application);
        skip.setValue("跳过");
        brand.setValue("本应用封面图为手绘图书馆团队作品");
        img.setValue("");
    }
    public void setBinding(com.onlinelibrary.databinding.ActivityWelcomeBinding binding, WelcomeActivity welcomeActivity) {
        WelcomeVM.welcomeActivity=welcomeActivity;
        handler.postDelayed(runnable, 0);
        img.postValue(Http.BASE_URL+"welcome/getWelcome/");
    }
    @BindingAdapter("bindImg")
    public static void bindImg(ImageView imageView,MutableLiveData<String> img){
        if (welcomeActivity!=null&&img.getValue()!=null&&!img.getValue().equals("")){
            Glide.with(welcomeActivity).load(img.getValue()).into(imageView);
        }
    }
    public static void skip(View view){
        welcomeActivity.startActivity(new Intent(welcomeActivity, MainActivity.class));
        welcomeActivity.finish();
    }
    public void onDestroy() {
        handler.removeCallbacks(runnable);
    }
}
